package com.koron.standard.deviceClasssify.bean.excel;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.koron.common.core.util.CodeTools;
import com.koron.standard.deviceClasssify.bean.po.TypeParams;
import com.koron.standard.deviceClasssify.excel.TypeStringStringConvertor;
import lombok.Data;
import lombok.ToString;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/6/6
 */
@ToString
@Data
@ExcelIgnoreUnannotated
public class TypeParamsExcel {

    // 原1.0系统使用
    // @ExcelProperty(value = "字母编号")
    private String letter;

    @ExcelProperty(value = "层级编号")
    private String order;

    @ExcelProperty(value = "分类编码")
    private String code;

    @ExcelProperty(value = "参数名称")
    private String paramName;

    @ExcelProperty(value = "数据类型", converter = TypeStringStringConvertor.class)
    private String dataType;

    @ExcelProperty(value = "数据长度")
    private String dataLength;

    @ExcelProperty(value = "精度")
    private String dataPrecision;

    @ExcelProperty(value = "输入类型", converter = TypeStringStringConvertor.class)
    private String enterType;

    @ExcelProperty(value = "是否必填", converter = TypeStringStringConvertor.class)
    private String isMust;

    @ExcelProperty(value = "数据计量单位")
    private String dataUnit;

    @ExcelProperty(value = "参数说明")
    private String desc;

    @ExcelProperty(value = "参数列表")
    private String dataText;

    public String getCode() {
        // 分类编码 > 层级编号 > 字母编号
        // 分类编码和字母编号二选一，两者都有时以分类编码为准
        if (code == null && letter != null) {
            char[] chars = letter.toCharArray();
            StringBuilder sb = new StringBuilder();
            for (char aChar : chars) {
                sb.append(String.format("%03d", aChar - 'A' + 1));
            }
            code = sb.toString();
        }

        // 层级编号和分类编码二选一，两者都有时以分类编码为准
        if (code == null && order != null) {
            code = Arrays.stream(order.split("\\."))
                    .map(e -> String.format("%03d", Integer.valueOf(e)))
                    .collect(Collectors.joining());
        }
        return code;
    }

    public TypeParams buildParam(String preTypeId, Integer sort) {
        TypeParams typeParams = new TypeParams();
        // id
        typeParams.setId(CodeTools.getCode32());
        typeParams.setGroup("2"); // 都是技术参数
        typeParams.setTypeId(preTypeId);

        // 基本信息
        typeParams.setParamName(getParamName());
        typeParams.setIsMust(NumberUtil.parseInt(getIsMust()));
        typeParams.setEnterType(getEnterType());
        typeParams.setDataType(getDataType());
        typeParams.setDataLength(NumberUtil.parseInt(getDataLength()));
        typeParams.setDataPrecision(NumberUtil.parseInt(getDataPrecision()));
        typeParams.setDataUnit(getDataUnit());
        typeParams.setDataText(getDataText());
        typeParams.setDesc(getDesc());
        typeParams.setSort(sort);
        return typeParams;
    }
}
